package main;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;

import javax.swing.JFrame;

import Managing.GameKeys;
import Managing.InputHandle;
import Screens.Screen;
import Volts.Player;

public class Main{
	private final String title = "Dop";
	private final static int WIDTH = 640;
	private final static int HEIGHT = 480;
	private static Dimension size = new Dimension(WIDTH,HEIGHT);
	public JFrame frame = new JFrame(title);
	static Screen screen;
	static boolean running = false;
	static InputHandle ih;
	private static boolean playerDead = false;
	public static int getWidth() {
		return WIDTH;
	}

	public static int getHeight() {
		return HEIGHT;
	}

	public static Dimension getSize() {
		return size;
	}

	public static void main(String[] args){
		running = true;
		new Main();
		ih = new InputHandle(screen);
		run();
		
		
		
	}
	public static void run() {
		long lastTime = System.nanoTime();
		double nsPerTick = 1000000000D/60D;
		
		long lastTimer = System.currentTimeMillis();
		double delta = 0;
		while(running){
			long now = System.nanoTime();
			delta += (now - lastTime)/nsPerTick;
			lastTime = now;
			boolean shouldRender = true;
			
			
			
			while (delta >= 1){
				tick();
				delta-=1;
				shouldRender = true;
			}

			if (shouldRender){
			render();
			}
			if (System.currentTimeMillis() - lastTimer >= 1000){
				lastTimer += 1000;
			}
		}
	}
	static void tick(){
		
		
	}
	private static BufferedImage image = new BufferedImage(WIDTH,HEIGHT, BufferedImage.TYPE_INT_RGB);
	@SuppressWarnings("unused")
	private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
	static void render(){
		BufferStrategy bs = screen.getBufferStrategy();
		if (bs == null){
			screen.createBufferStrategy(4);
			return;
		}
		
		Graphics g = bs.getDrawGraphics();
		g.drawImage(image, 0, 0, WIDTH, HEIGHT, null);
		screen.paintComponent(g);
		g.dispose();
		bs.show();
		if(!playerDead){
			if(GameKeys.isPressed(GameKeys.UP)){
				if(Main.screen.battleGround[Player.x][Player.y-1].equals("*")){
					
				}else{
					Main.screen.battleGround[Player.x][Player.y] = "0";
					Player.y--;
					Main.screen.battleGround[Player.x][Player.y] = "P";
				}
				Main.Update();
			}else if(GameKeys.isPressed(GameKeys.DOWN)){
				
				if(Main.screen.battleGround[Player.x][Player.y+1].equals("*")){
					
				}else{
					Main.screen.battleGround[Player.x][Player.y] = "0";
					Player.y++;
					Main.screen.battleGround[Player.x][Player.y] = "P";
				}
				Main.Update();
			}else if(GameKeys.isPressed(GameKeys.RIGHT)){
				if(Main.screen.battleGround[Player.x+1][Player.y].equals("*")){
					
				}else{
					Main.screen.battleGround[Player.x][Player.y] = "0";
					Player.x++;
					Main.screen.battleGround[Player.x][Player.y] = "P";
				}
				Main.Update();
			}else if(GameKeys.isPressed(GameKeys.LEFT)){
				
				if(Main.screen.battleGround[Player.x-1][Player.y].equals("*")){
					
				}else{
					Main.screen.battleGround[Player.x][Player.y] = "0";
					Player.x--;
					Main.screen.battleGround[Player.x][Player.y] = "P";
				}
				Main.Update();
			}
		}
		
		GameKeys.update();
		
		
	}
	private final static int UP = 0;
	private final static int RIGHT = 1;
	private final static int DOWN = 2;
	private final static int LEFT = 3;
	//private static int i = 0;
	private static int dir = LEFT;
	private static int UD = UP;
	
	public static void Update(){
		//System.out.println(i);
		//i++;
		for(int i = 0; i < screen.monsters.size(); i++){
			if(isPlayerInArea(screen.monsters.get(i).x-4, screen.monsters.get(i).x+4,screen.monsters.get(i).y-4, screen.monsters.get(i).y+4)){
				if(Player.y > screen.monsters.get(i).y){//ako e nad PLEIARA
					trySmartMove(DOWN,i);
				}else if(Player.y < screen.monsters.get(i).y){//ako e pod PLEIARA
					trySmartMove(UP,i);
				}else if(Player.y == screen.monsters.get(i).y){ // ako e na nivoto na pleira
					
					if(Player.x < screen.monsters.get(i).x){ // ako e nalqvo ot teb
						trySmartMove(LEFT, i);
						
					}else if(Player.x > screen.monsters.get(i).x){ // ako e nadqsno ot teb
						trySmartMove(RIGHT, i);
		
					}
				}
			}else{
				int randomDir = (int)(Math.random()*4);
				if(randomDir == UP){
					tryMove(UP,i);
				}else if(randomDir == RIGHT){
					tryMove(RIGHT,i);
				}else if(randomDir == DOWN){
					tryMove(DOWN,i);
				}else if(randomDir == LEFT){
					tryMove(LEFT,i);
				}
			}
			if(Main.screen.battleGround[screen.monsters.get(i).x][screen.monsters.get(i).y].equals(Main.screen.battleGround[Player.x][Player.y])){
				playerDead =true;
				Main.screen.battleGround[Player.x][Player.y] = "M";
			}
		}
		
		
	}
	
	private static boolean isPlayerInArea(int areaX1, int areaX2, int areaY1, int areaY2){
		if((Player.x >= areaX1 && Player.x <= areaX2) && (Player.y >=areaY1 && Player.y <= areaY2)){
			return true;
		}else{
			return false;
		}
	}
	private static void trySmartMove(int b, int i){
		if(b == UP){
			/////////////
			if(Main.screen.battleGround[screen.monsters.get(i).x][screen.monsters.get(i).y-1].equals("*")){
				
				if(dir == RIGHT){
					if(!Main.screen.battleGround[screen.monsters.get(i).x+1][screen.monsters.get(i).y].equals("*")){
						tryMove(RIGHT, i);
						dir = RIGHT;
					}else if(!Main.screen.battleGround[screen.monsters.get(i).x-1][screen.monsters.get(i).y].equals("*")){
						tryMove(LEFT, i);
						dir = LEFT;
					}
				}else if(dir == LEFT){
					if(!Main.screen.battleGround[screen.monsters.get(i).x-1][screen.monsters.get(i).y].equals("*")){
						tryMove(LEFT, i);
						dir = LEFT;
					}else if(!Main.screen.battleGround[screen.monsters.get(i).x+1][screen.monsters.get(i).y].equals("*")){
						tryMove(RIGHT, i);
						dir = RIGHT;
					}
				}
				
			}else{
				tryMove(UP,i);
				UD = UP;
			}
			///////////
		}else if(b == RIGHT){
			///////////
			if(Main.screen.battleGround[screen.monsters.get(i).x+1][screen.monsters.get(i).y].equals("*")){
				
				if(UD == UP){
					if(!Main.screen.battleGround[screen.monsters.get(i).x][screen.monsters.get(i).y+1].equals("*")){
						trySmartMove(UP, i);
						UD = UP;
					}else if(!Main.screen.battleGround[screen.monsters.get(i).x][screen.monsters.get(i).y-1].equals("*")){
						trySmartMove(DOWN, i);
						UD = DOWN;
					}
				}else if(UD == DOWN){
					if(!Main.screen.battleGround[screen.monsters.get(i).x][screen.monsters.get(i).y-1].equals("*")){
						trySmartMove(DOWN, i);
						UD = DOWN;
					}else if(!Main.screen.battleGround[screen.monsters.get(i).x][screen.monsters.get(i).y+1].equals("*")){
						trySmartMove(UP, i);
						UD = UP;
					}
				}
				
			}else{
				tryMove(RIGHT, i);
				dir=RIGHT;
			}
			//////////
		}else if(b == DOWN){
			/////////////
			if(Main.screen.battleGround[screen.monsters.get(i).x][screen.monsters.get(i).y+1].equals("*")){
				
				if(dir == RIGHT){
					if(!Main.screen.battleGround[screen.monsters.get(i).x+1][screen.monsters.get(i).y].equals("*")){
						tryMove(RIGHT, i);
						dir = RIGHT;
					}else if(!Main.screen.battleGround[screen.monsters.get(i).x-1][screen.monsters.get(i).y].equals("*")){
						tryMove(LEFT, i);
						dir = LEFT;
					}
				}else if(dir == LEFT){
					if(!Main.screen.battleGround[screen.monsters.get(i).x-1][screen.monsters.get(i).y].equals("*")){
						tryMove(LEFT, i);
						dir = LEFT;
					}else if(!Main.screen.battleGround[screen.monsters.get(i).x+1][screen.monsters.get(i).y].equals("*")){
						tryMove(RIGHT, i);
						dir = RIGHT;
					}
				}
			
			}else{
				tryMove(DOWN,i);
				UD = DOWN;
			}
			///////////
		}else if(b == LEFT){
			//////////
			if(Main.screen.battleGround[screen.monsters.get(i).x-1][screen.monsters.get(i).y].equals("*")){
				
				if(UD == UP){
					if(!Main.screen.battleGround[screen.monsters.get(i).x][screen.monsters.get(i).y+1].equals("*")){
						trySmartMove(UP, i);
						UD = UP;
					}else if(!Main.screen.battleGround[screen.monsters.get(i).x][screen.monsters.get(i).y-1].equals("*")){
						trySmartMove(DOWN, i);
						UD = DOWN;
					}
				}else if(UD == DOWN){
					if(!Main.screen.battleGround[screen.monsters.get(i).x][screen.monsters.get(i).y-1].equals("*")){
						trySmartMove(DOWN, i);
						UD = DOWN;
					}else if(!Main.screen.battleGround[screen.monsters.get(i).x][screen.monsters.get(i).y+1].equals("*")){
						trySmartMove(UP, i);
						UD = UP;
					}
				}
				
			}else{
				tryMove(LEFT,i);
				dir=LEFT;
			}
			///////////
		}
		
		Main.screen.battleGround[screen.monsters.get(i).x][screen.monsters.get(i).y] = "M";
	}
	private static void tryMove(int b, int i){
		if(b == UP){
			/////////////
			if(Main.screen.battleGround[screen.monsters.get(i).x][screen.monsters.get(i).y-1].equals("*") || Main.screen.battleGround[screen.monsters.get(i).x][screen.monsters.get(i).y-1].equals("M")){
				
			}else{
				Main.screen.battleGround[screen.monsters.get(i).x][screen.monsters.get(i).y] = "0";
				screen.monsters.get(i).y--;
			}
			///////////
		}else if(b == RIGHT){
			///////////
			if(Main.screen.battleGround[screen.monsters.get(i).x+1][screen.monsters.get(i).y].equals("*") || Main.screen.battleGround[screen.monsters.get(i).x+1][screen.monsters.get(i).y].equals("M")){
				
			}else{
				Main.screen.battleGround[screen.monsters.get(i).x][screen.monsters.get(i).y] = "0";
				screen.monsters.get(i).x++;
			}
			//////////
		}else if(b == DOWN){
			/////////////
			if(Main.screen.battleGround[screen.monsters.get(i).x][screen.monsters.get(i).y+1].equals("*") || Main.screen.battleGround[screen.monsters.get(i).x][screen.monsters.get(i).y+1].equals("M")){
				
			}else{
				Main.screen.battleGround[screen.monsters.get(i).x][screen.monsters.get(i).y] = "0";
				screen.monsters.get(i).y++;
			}
			///////////
		}else if(b == LEFT){
			//////////
			if(Main.screen.battleGround[screen.monsters.get(i).x-1][screen.monsters.get(i).y].equals("*") || Main.screen.battleGround[screen.monsters.get(i).x-1][screen.monsters.get(i).y].equals("M")){
				
			}else{
				Main.screen.battleGround[screen.monsters.get(i).x][screen.monsters.get(i).y] = "0";
				screen.monsters.get(i).x--;
			}
			///////////
		}
		
		Main.screen.battleGround[screen.monsters.get(i).x][screen.monsters.get(i).y] = "M";
	}
	public Main(){
		frame.setSize(size);
		screen = new Screen();
		frame.add(screen);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
	
}
